/* eslint-disable no-undef */
const InfoWindow = function (earth, divId) {
  // 随机生成UUID
  const S4 = function () {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  };
  const guid = function () {
    return (`${S4() + S4()}-${S4()}-${S4()}-${S4()}-${S4()}${S4()}${S4()}`);
  };

  const viewer = earth;
  const { scene } = earth;
  const _uuid = `id${guid()}`;
  let position = null;
  let content = '';
  let resultPosition = null;
  let infoWindowHandler = null;

  const childContainer = document.getElementById(divId);
  // var childContainer = document.createElement('div');
  if(childContainer)childContainer.id = divId;

  const removeDiv = function () {
    if (childContainer&&document.getElementById(childContainer.id) === null) return;
    try {
      childContainer.style.display = 'none';
      if (infoWindowHandler) { 
        infoWindowHandler.call();
        scene.postRender.removeEventListener(_updateTracking, this);
      }
    } catch (e) {
      console.warn(e);
    }
  };

  // 监听地球转动事件
  const _updateTracking = function () {
    // console.log(1);
    const changedC = ToWindowCoordinates();//坐标转换
    if (changedC) {
      if ((position.x !== changedC.x) || (position.y !== changedC.y)) {
        positionPopUp(changedC);//修改窗口位置
        position = changedC;
      }
    }
  };
  // 给定气泡位置
  var positionPopUp = function (c) {
    const div = document.getElementById(childContainer.id);
    const x = c.x - (div.offsetWidth) / 2;
    const y = c.y - (div.offsetHeight);
    childContainer.style.left = `${x}px`;
    const resultTop = y - 150;
    childContainer.style.top = `${y - 10}px`;
    const value = (div.offsetHeight + (y - 10));
    // updateTopPosition(resultTop, value);
  };

  var ToWindowCoordinates = function () {
    // 地球转动后，坐标的相较于屏幕的偏移量（原来GIS坐标在屏幕坐标的100px,20px,地球转动后
    // GIS原坐标对应屏幕的坐标变为200px,600px)
    return Cesium.SceneTransforms.wgs84ToWindowCoordinates(scene, resultPosition);
  };
  // 判断是否到达顶部
  const updateTopPosition = function (resultTop, value) {
    let div1 = document.getElementById('searchInput');
    const div = document.getElementById(childContainer.id);
    if (resultTop < 0) {
      if(div1){
        let obj = div1.getBoundingClientRect();
        div.style.top = `${obj.top+div1.offsetHeight+10}px`;
      }
      //div.style.top = `${value}px`;
     
    }
  };

  // 创建像素的弹出窗
  const createPXWindow = function () {
    const left = position.x;
    const top = position.y;
    const div = document.getElementById(childContainer.id);
    childContainer.style.left = `${left - (div.offsetWidth) / 2}px`;
    const resultTop = top - div.offsetHeight - 10;
    childContainer.style.top = `${resultTop}px`;

  };
    // 创建经纬度的弹出窗，可能会随地球移动
  const createPositionWindow = function (infoMove) {
    // 记录首次弹出窗体的位置并记录此位置，转为屏幕坐标
    // resultPosition = viewer.camera.pickEllipsoid(position);
    resultPosition = position;
    const Cartesian2 = ToWindowCoordinates();
    positionPopUp(Cartesian2);
    if (infoMove == true) { infoWindowHandler = scene.postRender.addEventListener(_updateTracking, this); }
  };

  this.setContent = function (_content, _position, top) {
    content = _content;
    if (top) {
      position = {
        x: _position,
        y: top,
      };
    } else { position = viewer.camera.pickEllipsoid(_position); }
  };
  // 实时改变窗口位置
  this.setPositon = function (_position) {
    positionPopUp(_position);
  };
  this.childContainer = function () {
    return childContainer.id;
  };
  this.setContentconTainHeigh = function (_content, _position) {
    content = _content;
    position = _position;
  };

  this.createInfoWindow = function (value, id) {

    childContainer.style.display = 'block';
    // childContainer.style.height = (contentLink.offsetHeight + 50) + 'px';
    if (value && value == true) { createPositionWindow(value); } else { createPXWindow(); }
  };
  this.remove = function () {
    removeDiv();
  };
  this.setSize = function (_width, _height) {
    childContainer.style.width = `${_width}px`;
    childContainer.style.height = `${_height}px`;
  };
  // 当前窗口的显隐，显示true，隐藏false
  this.isVisible = function (value) {
    if (value) { close_popup.style.display = 'block'; } else { close_popup.style.display = 'none'; }
  };
};
export default InfoWindow;
